<?php
/**
 * Created by PhpStorm.
 * User: tao
 * Date: 2016/11/30
 * Time: 下午2:11
 */

namespace Admin\Model;
use Think\Model;
use PHPExcel_IOFactory;
use PHPExcel;

class StudentModel extends Model {
    public function readExcel($filename, $exts='xls')
    {
    	require_once './ThinkPHP/Library/Vendor/PHPExcel/PHPExcel/IOFactory.php';  
        //导入PHPExcel类库，因为PHPExcel没有用命名空间，只能inport导入
        import("Org.Util.PHPExcel");
        //创建PHPExcel对象，注意，不能少了\
        $PHPExcel=new \PHPExcel();
        //如果excel文件后缀名为.xls，导入这个类
        if($exts == 'xls'){
            import("Org.Util.PHPExcel.Reader.Excel5");
            $PHPReader=new \PHPExcel_Reader_Excel5();
        }else if($exts == 'xlsx'){
            import("Org.Util.PHPExcel.Reader.Excel2007");
            $PHPReader=new \PHPExcel_Reader_Excel2007();
        }else if($exts == 'csv'){
            import("Org.Util.PHPExcel.Reader.CSV");
            $PHPReader=new \PHPExcel_Reader_CSV();
        }
 
       // var_dump($PHPReader);die;
        //载入文件
        $PHPExcel=$PHPReader->load($filename);
        //获取表中的第一个工作表，如果要获取第二个，把0改为1，依次类推
        $currentSheet=$PHPExcel->getSheet(0);
        //获取总列数
        $allColumn=$currentSheet->getHighestColumn();
        //获取总行数
        $allRow=$currentSheet->getHighestRow();
        //循环获取表中的数据，$currentRow表示当前行，从哪行开始读取数据，索引值从0开始
        for($currentRow=1;$currentRow<=$allRow;$currentRow++){
            //从哪列开始，A表示第一列
            for($currentColumn='A';$currentColumn<=$allColumn;$currentColumn++){
                //数据坐标
                $address=$currentColumn.$currentRow;
                //读取到的数据，保存到数组$arr中
                $data[$currentRow][$currentColumn]=$currentSheet->getCell($address)->getValue();
            }
 
        }
        return $data;
    }

	public function saveExcel($data){
		foreach($data as $key=>$a){
			if($key==1){
				continue;
			}
			$data['idCard'] = $a['A'];
			$data['studentID'] = $a['B'];
			$data['studentName'] = $a['C'];
			$data['forecast'] = $a['D'];
			$data['studentSex'] = $a['E'];
			$data['studentNumber'] = $a['F'];
			$data['dormitoryGrade'] = $a['G'];
			$data['dormitory'] = $a['H'];
			$data['report'] = $a['I'];
			$data['address'] = $a['J'];
			$data['sick'] = $a['K'];
			$data['prove'] = $a['L'];
			$data['insurance'] = $a['M'];
			$data['className'] = $a['N'];
			$data['speName'] = $a['O'];
			M('student')->data($data)->add();
		}
	}
}
